﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SqlServerDataTableToExcel.views
{
    public partial class DataItem : UserControl
    {
        public event Action<DataItem> delEvent;

        private Model.Item_Model itemMsg { get; set; }

        public DataItem(Model.Item_Model _itemMsg)
        {
            InitializeComponent();

            itemMsg = _itemMsg;
            //   btnDel.
            BindMsg();


        }



      


        private void BindMsg()
        {
          
            lbName.Text = itemMsg.Name;
            lbAddress.Text = itemMsg.IpAddress;
            lbDataSource.Text = itemMsg.DataSource;
            lbType.Text = itemMsg.Type;
        }


        private void btnMenu_Click(object sender, EventArgs e)
        {
            int X = 0;
            int Y = 0;
            X = btnMenu.Location.X + btnMenu.Size.Width + 4;
            Y = btnMenu.Location.Y;
            Point cmLocation = new Point(X, Y);
            cMBtn.Show(panel1, cmLocation);
           
        }

        private void btnEdit_MouseHover(object sender, EventArgs e)
        {
            btnEdit.BackColor = Color.FromArgb(45, 45, 48);
        }

        private void DataItem_DoubleClick(object sender, EventArgs e)
        {
            frmFileSelect frmExcel = new frmFileSelect(itemMsg);
            frmExcel.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            frmFileEdit frm = new frmFileEdit(itemMsg);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                itemMsg = frm.objItem;
                BindMsg();
            }
                

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show(this, "确定需要删除吗？", "确定", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    return;
                }

                BLL.LocalDs.Del(itemMsg);
                if(delEvent != null)
                {
                    delEvent.Invoke(this);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lbType_DoubleClick(object sender, EventArgs e)
        {
            this.OnDoubleClick(e);
        }
    }
}
